home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c
- Subject: Re: Turbo C 3.0 - arithmetic in defines
- Date: 15 Feb 1996 17:12:26 GMT
- Organization: Borland International
- Message-ID: <4fvphq$t06@druid.borland.com>
- References: <4fqgkf$iqd@ccshst05.cs.uoguelph.ca> <4fre9v$kcs@news.nstn.ca>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <4fre9v$kcs@news.nstn.ca>, keichele@ac.dal.ca says...
- >
- >thay@uoguelph.ca (Toby K Hay) wrote:
- >
- >>As a newcomer to C programming I'm having difficulty using arithmetic
- >>and #defined values to dimension arrays at compile time. The following
- >>very short program reproduces my error:
- >
- >>#define var1 16
- >>#define var2 485
- >>#define var3 ((var2-1)/var1)+1
- >>int main(void)
- >> {
- >> printf("\n%i %i %i %i",var1,var2,var3,var1*var3);
- >> return 0;
- >> }
- >
- >>Output from this is '16 485 31 481', not '16 485 31 496' as I should have
- >>expected. That is, var3 is calculated correctly in its #define, but the
- >>var1*var3 in the printf() statement is not calculated correctly. Where
- >>am I going wrong?
- >>Toby Hay thay@uoguelph.ca
- >
- >Hi,
- >you need one more set of parentheses for your definition of var3:
-
- Nope. The expression has the right number of parentheses. The problem is
- that they're in the wrong places.
-
- > #define var3 (((var2-1)/var1)+1)
-
- #define var3 ((var2-1)/var1+1)
-
-